package pers.cz.business;

import cn.hutool.core.date.DateUtil;
import pers.cz.mapper.RequestRecordMapper;
import pers.cz.model.RequestRecord;
import pers.cz.pub.AbstractBusiness;

import java.util.List;

/**
 * 测试工具:接口请求
 * @program: PostGirl-panent
 * @description: SingleRequestBusiness
 * @author: Cheng Zhi
 * @create: 2021-06-14 10:11
 **/
public class SingleRequestBusiness extends AbstractBusiness {

    /**
     * 获取最近25条请求记录
     * @return
     */
    public List<RequestRecord> getRequestRecord() {

        List<RequestRecord> list = this.getMapper(RequestRecordMapper.class).selectTopRequestRecord();
        return list;
    }

    /**
     * 将请求记录插入数据库
     * @param userId 用户ID
     * @param url 请求url
     * @param request 请求内容
     * @param type 请求类型
     * @param originate 请求起源，这里指body或者params
     */
    public void insertRequestRecord(Integer userId, String url, String request, String type, String originate) {

        RequestRecord requestRecord = new RequestRecord();
        requestRecord.setUserId(userId);
        requestRecord.setRequest(request);
        requestRecord.setType(type);
        requestRecord.setUrl(url);
        requestRecord.setRequestDate(DateUtil.date());
        requestRecord.setMethod(originate);
        try {
            this.getMapper(RequestRecordMapper.class).insert(requestRecord);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
